package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.UserSessionUtils;
import com.example.demo.entity.Articleinfo;
import com.example.demo.entity.CommentInfo;
import com.example.demo.entity.Userinfo;
import com.example.demo.mapper.CommentMapper;
import com.example.demo.service.ArticleService;
import com.example.demo.service.CommentService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2023-09-15
 * Time:21:53
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentMapper commentService;

    @Autowired
    private UserService userService;

    @Autowired
    private ArticleService articleService;


    @RequestMapping("/add")
    public AjaxResult add(HttpServletRequest request, CommentInfo commentInfo){
        if (commentInfo == null || !StringUtils.hasLength(commentInfo.getComment())){
            // 非空校验
            return AjaxResult.fail(-1,"参数错误");
        }
        Userinfo userinfo = UserSessionUtils.getSessionUser(request);
        if(userinfo == null || !StringUtils.hasLength(userinfo.getUsername())) {
            return AjaxResult.fail(-2, "请登录后评论!");
        }
        //设置 comment 中内容
        commentInfo.setUid(userinfo.getId());
        commentInfo.setNickname(userinfo.getUsername());
        commentInfo.setPhoto(userinfo.getPhoto());
        return AjaxResult.success(commentService.add(commentInfo));
    }

    @RequestMapping("/show")
    public AjaxResult show(Integer aid) {
        //非空校验
        if(aid <= 0) {
            return AjaxResult.fail(-1, "参数错误!");
        }
        //通过文章 id 获取所有该文章的评论
        List<CommentInfo> list = commentService.showComment(aid);
        return AjaxResult.success(list);
    }

    /**
     * 删除评论
     * @param request
     * @param id 评论id
     * @param aid 文章id
     * @return
     */
    @RequestMapping("del")
    public AjaxResult del(HttpServletRequest request,Integer id,Integer aid){
        // 非空校验
        if (id == null || aid == null){
            return AjaxResult.fail(-1,"你无权删除该评论!1");
        }
        /**
         * 只有文章作者和发布评论的用户才能删除评论
         * 评论删除具备条件:
         * 1.当前用户 id 等于 评论uid
         * 2.当前文章作者信息的 id 需要等于当前用户的 id
         */
        //获取当前用户信息
        Userinfo userinfo = UserSessionUtils.getSessionUser(request);
        if (userinfo == null || !StringUtils.hasLength(userinfo.getUsername()) ){
            return AjaxResult.fail(-2,"你无权删除该评论!2");
        }
        //获取作者信息
        Articleinfo articleinfo = articleService.getDetail(aid);
        if (articleinfo == null){
            return AjaxResult.fail(-2,"你无权删除该评论!3");
        }
        //获取评论信息
        CommentInfo commentInfo = commentService.getCommonentById(id);
        if (commentInfo == null || !StringUtils.hasLength(commentInfo.getNickname()) || !StringUtils.hasLength(commentInfo.getComment())){
            return AjaxResult.fail(-3,"你无权删除该评论!4");
        }

        //当满足条件就可删除
        if (userinfo.getId().equals(commentInfo.getUid()) || articleinfo.getUid().equals(commentInfo.getAid())){
            //可以进行删除
            return AjaxResult.success(commentService.del(id));
        }else {
            //不可以进行删除
            return AjaxResult.fail(-4,"你无权进行删除!");
        }
    }

}
